import Vue from 'vue'
import Router from 'vue-router'
// import { MessageBox } from 'element-ui'
// import { setToken, getToken } from '@/utils/auth'
// import { refreshToken } from '@/api/user'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/* Router Modules */

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    noCache: true                if set true, the page will no be cached(default is false)
    affix: true                  if set true, the tag will affix in the tags-view
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  {
    path: '/personal',
    component: Layout,
    hidden: true,
    redirect: '/personal',
    children: [
      {
        path: '/personal',
        component: () => import('@/views/personal/index'),
        name: 'personal',
        meta: { title: '个人中心', noCache: true }
      }
    ]
  },
  {
    path: '/',
    component: Layout,
    redirect: '/home',
    children: [
      {
        path: '/home',
        component: () => import('@/views/home/index'),
        name: 'home',
        meta: { title: '主页', icon: 'home', affix: true }
      }
    ]
  },
  // 资金管理
  {
    path: '/capital',
    component: Layout,
    name: 'capital',
    meta: {
      title: '资金管理',
      icon: 'capital'
    },
    children: [
      {
        path: '/capital/application',
        component: () => import('@/views/capital/application'),
        name: 'application',
        meta: { title: '入账申请', noCache: true }
      },
      {
        path: '/capital/entry',
        component: () => import('@/views/capital/entry'),
        name: 'entry',
        meta: { title: '入账记录', noCache: true }
      },
      {
        path: '/capital/select',
        component: () => import('@/views/capital/select'),
        name: 'select',
        meta: { title: '资金查询', noCache: true }
      }
    ]
  },
  // 旅游商品
  {
    path: '/travel',
    component: Layout,
    name: 'travel',
    meta: {
      title: '旅游商品',
      icon: 'travel'
    },
    children: [
      {
        path: '/travel/ticket/oneDay',
        name: 'ticketCalendar',
        alwaysShow: true,
        component: () => import('@/views/travel/ticket/oneDay/index'),
        meta: { title: '门票商品', noCache: true },
        children: [
          {
            path: '/travel/ticket/oneDay/calendar',
            component: () => import('@/views/travel/ticket/oneDay/calendar'),
            name: 'calendar',
            meta: { title: '单日门票', noCache: true }
          },
          {
            path: '/travel/ticket/oneDay/issue',
            component: () => import('@/views/travel/ticket/oneDay/issue'),
            name: 'issue',
            meta: { title: '单日门票', noCache: true },
            hidden: true
          },
          {
            path: '/travel/ticket/quarterAndYear/index',
            component: () => import('@/views/travel/ticket/quarterAndYear/index'),
            name: 'quarterAndyear',
            meta: { title: '年卡季卡', noCache: true }
          }
        ]
      },
      {
        path: '/travel/hotel',
        name: 'hotel',
        component: () => import('@/views/travel/hotel'),
        meta: { title: '酒店商品', noCache: true }
      },
      {
        path: '/travel/selfSupport',
        name: 'selfSupport',
        component: () => import('@/views/travel/selfSupport'),
        meta: { title: '自营商品', noCache: true }
      },
      {
        path: '/travel/holdOrder',
        component: () => import('@/views/travel/holdOrder/index.vue'),
        name: 'holdOrder',
        meta: { title: '暂存订单', noCache: true }
      }
    ]
  },
  // 订单管理
  {
    path: '/order',
    component: Layout,
    alwaysShow: true,
    name: 'order',
    meta: { title: '订单管理', icon: 'order' },
    children: [
      {
        path: '/order/list',
        component: () => import('@/views/order/list'),
        name: 'ticket',
        meta: { title: '订单查询', noCache: true }
      }, {
        path: '/order/details',
        component: () => import('@/views/order/details'),
        hidden: true,
        name: 'orderDetails',
        meta: { title: '订单详情', noCache: true }
      }
    ]
  },
  // 小程序管理
  {
    path: '/smallProgram',
    component: Layout,
    alwaysShow: true,
    name: 'order',
    meta: { title: '小程序管理', icon: 'smallProgram' },
    children: [
      {
        path: '/smallProgram/index',
        component: () => import('@/views/smallProgram/index'),
        name: 'smallProgramSale',
        meta: { title: '销售管理', noCache: true }
      }, {
        path: '/smallProgram/details/index',
        component: () => import('@/views/smallProgram/details/index'),
        hidden: true,
        name: 'smallProgramDetails',
        meta: { title: '销售详情', noCache: true }
      }
    ]
  },
  // 子级商户
  {
    path: '/merchant',
    component: Layout,
    alwaysShow: true,
    name: 'merchant',
    meta: { title: '子级商户', icon: 'merchant' },
    children: [
      {
        path: '/merchant/administration',
        component: () => import('@/views/merchant/administration'),
        name: 'administration',
        meta: { title: '商户管理', noCache: true }
      }, {
        path: '/merchant/apply',
        component: () => import('@/views/merchant/apply'),
        name: 'merchantApply',
        meta: { title: '申请记录', noCache: true }
      },
      {
        path: '/merchant/profit',
        component: () => import('@/views/merchant/profit'),
        name: 'Profit',
        meta: { title: '门票通用利润', noCache: true }
      },
      {
        path: '/merchant/administration/multiplexing',
        component: () => import('@/views/merchant/administration/multiplexing'),
        name: 'multiplexing',
        hidden: true,
        meta: { title: '创建子级', noCache: true }
      },
      {
        path: '/merchant/administration/chakan',
        component: () => import('@/views/merchant/administration/chakan'),
        name: 'chakan',
        hidden: true,
        meta: { title: '查看详情', noCache: true }
      }
    ]
  },
  // 帮助中心
  {
    path: '/help',
    component: Layout,
    name: 'help',
    meta: {
      title: '帮助中心',
      icon: 'help'
    },
    children: [
      {
        path: '/help/notice/list',
        component: () => import('@/views/help/notice/list'),
        name: 'noticeList',
        meta: { title: '平台公告', noCache: true }
      },
      {
        path: '/help/notice/details',
        component: () => import('@/views/help/notice/details'),
        hidden: true,
        name: 'noticeDetails',
        meta: { title: '公告详情', noCache: true }
      },
      {
        path: '/help/gallery',
        component: () => import('@/views/help/gallery'),
        name: 'gallery',
        meta: { title: '图库查询', noCache: true }
      },
      {
        path: '/help/authorize',
        component: () => import('@/views/help/authorize'),
        name: 'authorize',
        meta: { title: '授权记录', noCache: true }
      }
    ]
  },
  { path: '*', redirect: '/404', hidden: true }
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
//
export const asyncRoutes = [

  /** when your routing map is too long, you can split it into small modules **/
  // componentsRouter,
  // chartsRouter,
  // nestedRouter,
  // tableRouter

]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

// 挂载路由导航守卫，相当于拦截器，所有路由请求都需要过一遍这个
// router.beforeEach((to, from, next) => {
// to => 将要访问的页面
// from => 代表从哪个路径跳转而来
// next=>是否放行 next()放行 next('/login')强制跳转到/login
// if (to.matched.length === 0) {
//   from.path ? next({ path: '*', redirect: '/404', hidden: true }) : next('/404')
// } else {
//   next() // 如果匹配到正确跳转
// }
// /login直接放行
// if (to.path === '/login') return next()
// // 其它判断是否有token
// const tokenstr = getToken()

// if (!tokenstr) {
//   return next('/login')
// } else {
//   refreshToken().then(res => {
//     if (res.data.code === 200) {
//       setToken(res.data.data.auth.token)
//       next()
//     }
//   }).catch(() => {
//     console.log(456)
//     MessageBox.confirm('身份验证已过期或失败，请重新登录', '提示', {
//       confirmButtonText: '去登录',
//       showCancelButton: false,
//       type: 'error',
//       callback: () => {
//         return next('/login')
//       }
//     })
//   })
// }
// })

export default router
